package analysis_pv;

import beans.PageViewCount;
import org.apache.flink.api.common.eventtime.SerializableTimestampAssigner;
import org.apache.flink.api.common.eventtime.WatermarkStrategy;
import org.apache.flink.api.common.functions.AggregateFunction;
import org.apache.flink.api.common.functions.MapFunction;
import org.apache.flink.api.common.state.ValueState;
import org.apache.flink.api.common.state.ValueStateDescriptor;
import org.apache.flink.api.java.tuple.Tuple2;
import org.apache.flink.configuration.Configuration;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.datastream.SingleOutputStreamOperator;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.api.functions.KeyedProcessFunction;
import org.apache.flink.streaming.api.functions.windowing.ProcessWindowFunction;
import org.apache.flink.streaming.api.windowing.assigners.TumblingEventTimeWindows;
import org.apache.flink.streaming.api.windowing.time.Time;
import org.apache.flink.streaming.api.windowing.windows.TimeWindow;
import org.apache.flink.util.Collector;

import java.util.Random;

/**
 * @author zkq
 * @date 2022/10/2 22:57
 */
public class PageView_Optimize {
    public static void main(String[] args) throws Exception {
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
        env.setParallelism(4);
        DataStreamSource<String> inputStream = env.readTextFile("F:\\javasecode220620\\UserBehaviorAnalysis\\NetworkFlowAnalysis\\src\\main\\resources\\UserBehavior.csv");
        SingleOutputStreamOperator<UserBehavior> Stream = inputStream
                .map(data -> {
                    String[] splits = data.split(",");
                    return new UserBehavior(new Long(splits[0]), new Long(splits[1]), new Integer(splits[2]),
                            splits[3], new Long(splits[4]));
                })
                .assignTimestampsAndWatermarks(WatermarkStrategy.<UserBehavior>forMonotonousTimestamps()
                        .withTimestampAssigner(new SerializableTimestampAssigner<UserBehavior>() {
                            @Override
                            public long extractTimestamp(UserBehavior element, long recordTimestamp) {
                                return element.getTimestamp() * 1000;
                            }
                        })
                );
        SingleOutputStreamOperator<PageViewCount> aggregate = Stream
                .filter(data -> "pv".equals(data.getBehavior()))
                .map(new MapFunction<UserBehavior, Tuple2<Integer, Long>>() {
                    @Override
                    public Tuple2<Integer, Long> map(UserBehavior value) throws Exception {
                        //随机数打散 分布式 提高并行度
                        int i = new Random().nextInt(10);
                        return Tuple2.of(i, 1L);
                    }
                })
                .keyBy(data -> data.f0)
                .window(TumblingEventTimeWindows.of(Time.hours(1)))
                .aggregate(new PvCount(), new PvCountResult());

        SingleOutputStreamOperator<PageViewCount> result = aggregate
                .keyBy(data -> data.getWindowEnd())
                //直接使用sum有点小问题 来一条处理一条会出现很多中间结果 可优化
                //.sum(1)
                .process(new KeyedProcessFunction<Long, PageViewCount, PageViewCount>() {

                    private ValueState<Long> state;

                    @Override
                    public void open(Configuration parameters) throws Exception {
                        //定义状态保存 count值
                        state = getRuntimeContext().getState(new ValueStateDescriptor<Long>("page-count", Long.class));
                    }

                    @Override
                    public void processElement(PageViewCount value, Context ctx, Collector<PageViewCount> out) throws Exception {
                        state.update(state.value()==null?0:state.value() + value.getCount());
                        //注册定时器，数据都到齐了统一计算
                        ctx.timerService().registerEventTimeTimer(value.getWindowEnd() + 1);
                    }

                    @Override
                    public void onTimer(long timestamp, OnTimerContext ctx, Collector<PageViewCount> out) throws Exception {
                        out.collect(new PageViewCount("pv", ctx.getCurrentKey(), state.value()));
                        state.clear();
                    }
                });
        result.print();

        env.execute();
    }
    public static class PvCount implements AggregateFunction<Tuple2<Integer, Long>,Long,Long>{

        @Override
        public Long createAccumulator() {
            return 0L;
        }

        @Override
        public Long add(Tuple2<Integer, Long> value, Long accumulator) {
            return accumulator + 1;
        }

        @Override
        public Long getResult(Long accumulator) {
            return accumulator;
        }

        @Override
        public Long merge(Long a, Long b) {
            return a + b;
        }
    }
    public static class PvCountResult extends ProcessWindowFunction<Long,PageViewCount,Integer, TimeWindow>{

        @Override
        public void process(Integer integer, Context context, Iterable<Long> elements, Collector<PageViewCount> out) throws Exception {
            out.collect(new PageViewCount(integer.toString(),context.window().getEnd(),elements.iterator().next()));
        }
    }
}
